// basicnpc.txt
// A very simple, naive text. Creature attacks anything it hates nearby.
// Once it has won, it returns to its home.
// Memory Cells:
//   Cell 0 - If 0, default behavior. If 1, it doesnt fidget.
//   Cell 1,2 - Stuff done flag. If both 0, nothing. Otherwise when this is killed, set to 1.
//   Cell 3,4,5 - The three creations it can summon. GIVE THE IN-GAME NUMBER
//   Cell 6 - Talking node

begincreaturescript;

variables;

short i,target;
short last_spawn;
short r1;
short last_abil;
short used_abil;
short res_level;
short which_ae = 0;
short start_move = 0;

body;

beginstate INIT_STATE;
	set_level(ME,35);
	change_max_health(ME,300);
	set_boss_level(ME,2);
	
	if (my_number() == 50) {
		set_name(ME,"Ghaldring");
		}
	if (my_number() == 75) {
		set_name(ME,"Rhakkus");
		}
	if (my_number() == 76) {
		set_name(ME,"Khrazass");
		}
		
	set_walk_speed(ME,0);
	
	last_spawn = get_current_tick();
	last_abil = get_current_tick();
	
	set_resistance(ME,7,100);
	break;

beginstate DEAD_STATE;
	if (my_number() == 50) {
		sf(101,10,1);
		begin_talk_mode(7);
		}

break;

beginstate START_STATE; 
	if ((start_move == 0) && (get_health(ME) < get_max_health(ME) / 2)) {
		print_named_str(ME,"becomes enraged. It begins to hunt.");
		set_walk_speed(ME,28);
		start_move = 1;
		}

	// if pc is not near, purge lighting, etc
	if (get_nearest_party_char(7) < 0) {
		if (get_char_status(ME,6) > 0) 
			set_char_status(ME,6,-4);
		if (get_char_status(ME,20) > 0)
			set_char_status(ME,20,-4);
		if (get_char_status(ME,2) > 0)
			set_char_status(ME,2,-4);
		}
		
	// if I have a target for some reason, go attack it
	if (target_ok()) {
			set_state(3);
		}
	
	if (get_foe_target(ME,8,0)) {
		do_attack();
		set_state(3);
		}
	if (who_shot_me() >= 0) {
		set_foe_target(ME,who_shot_me());
		do_attack();
		set_state(3);
		}

	if (tick_difference(last_spawn,get_current_tick()) > 4) {
		r1 = get_ran(1,0,2);
		last_spawn = get_current_tick();
		if ((get_memory_cell(3 + r1) > 0) && (char_ok(get_memory_cell(3 + r1)) == FALSE)) {
			if (summon_creature(get_memory_cell(3 + r1) - 8)) {
				set_summon_level(get_memory_cell(3 + r1),1);
				set_level(get_memory_cell(3 + r1),30);
				
				
				if (get_nearest_party_char(9) >= 0) {
					print_named_str(ME,"makes a new creation!");
					pc_heard_sound_delay(136,100);						
					place_particle_num(get_memory_cell(3 + r1),10,7,10);
					run_char_animation(2,1,50);	
					}

				end();
				}
			}
		}

	if ((get_attitude(ME) >= 10) && (start_move)) {
		set_foe_target(ME,random_group_member(0));
		set_state(3);
		}

	if (am_i_doing_action() == FALSE)
		end_combat_turn();
break;

beginstate 3; // attacking


	if (target_ok() == FALSE)
		set_state(START_STATE);

	// if pc is not near, purge lighting, etc
	if (get_nearest_party_char(7) < 0) {
		if (get_char_status(ME,6) > 0) 
			set_char_status(ME,6,-4);
		if (get_char_status(ME,20) > 0)
			set_char_status(ME,20,-4);
		if (get_char_status(ME,2) > 0)
			set_char_status(ME,2,-4);
		}
	
	if ((start_move == 0) && (get_health(ME) < get_max_health(ME) / 2)) {
		print_named_str(ME,"becomes enraged. It begins to hunt.");
		set_walk_speed(ME,28);
		start_move = 1;
		}

	if ((tick_difference(last_spawn,get_current_tick()) > 0) && (get_attitude(ME) >= 10) && 
	  (get_nearest_hate_char(8) >= 0)) {
		r1 = get_ran(1,0,2);
		if ((get_memory_cell(3 + r1) > 0) && (char_ok(get_memory_cell(3 + r1)) == FALSE)) {
			if (summon_creature(get_memory_cell(3 + r1) - 8)) {
				if (get_nearest_party_char(9) >= 0) 
					print_named_str(ME,"makes a new creation!");
				set_summon_level(get_memory_cell(3 + r1),1);
				place_particle_num(get_memory_cell(3 + r1),10,7,10);

				
				run_char_animation(2,1,50);	
				pc_heard_sound_delay(136,100);						
				
				last_spawn = get_current_tick();
				end();
				}
			}
		}
		
	if ((my_number() == 50) && (gf(61,12) < 2) && (get_health(ME) < (get_max_health(ME) * 5) / 6)) {
		sf(61,12,1);
		if (get_nearest_party_char(10) >= 0) {
			sf(61,12,2);
			begin_talk_mode(5);
			}
		}

	used_abil = 0;

	if ((my_number() == 50) && (tick_difference(last_abil,get_current_tick()) > 0) && (is_combat()) && (get_nearest_hate_char(8) >= 0)) {
		run_char_animation(2,1,35);	
		last_abil = get_current_tick();
			
		if (which_ae == 0) {
			which_ae = 1;
			r1 = get_ran(1,250,300);
			if (get_nearest_party_char(8) >= 0) {
				print_named_str(ME,"summons a cloud of fire.");
		  		create_missile_spiral(147,40,8,2);
				}
			pc_heard_sound_delay(179,250);						
	  		damage_nearby(r1,8,2,0);
			}
		else if (which_ae == 1) {
			which_ae = 2;
			r1 = get_ran(1,100,200);
		  	place_particle_num(ME,1,4,8);
			if (get_nearest_party_char(8) >= 0) {
				print_named_str(ME,"summons a cloud of searing energy.");
	  			create_missile_spiral(155,40,6,2);
	  			}
			pc_heard_sound_delay(179,250);						
	  		damage_nearby(r1,6,1,0);
			status_nearby(5,8,6,0);
			}
		else if (which_ae == 2) {
			which_ae = 0;
		  	place_particle_num(ME,3,6,8);
			if (get_nearest_party_char(8) >= 0) {
				print_named_str(ME,"summons a cloud of static.");
				spray_missiles(43,8,8);
				}
			pc_heard_sound_delay(201,250);						
			particle_nearby(8,8,2,4,2);
			status_nearby(-25,8,9,0);
			status_nearby(-25,8,11,0);
			status_nearby(-25,8,12,0);
			status_nearby(-25,8,13,0);
			status_nearby(-25,8,14,0);
			status_nearby(-25,8,15,0);
			status_nearby(-25,8,16,0);
			status_nearby(-25,8,18,0);
			status_nearby(-25,8,19,0);

			}
		}
		
	if (used_abil)
		end();


	do_attack();
break;

beginstate TALKING_STATE;
	if (get_memory_cell(6) == 0) {
		print_str("Talking: You make a bit of small talk, but you don't learn");
		print_str("  anything interesting.");
		}
		else begin_talk_mode(get_memory_cell(6));
break;